package com.h.heal.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h.heal.common.MyPageInfo;
import com.h.heal.entity.Topic;
import com.h.heal.entity.User;
import com.h.heal.mapper.TopicMapper;
import com.h.heal.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    private TopicMapper topicMapper;

    @Override
    public List<Topic> findAll() {
        return topicMapper.findAll();
    }

    @Override
    public void deleteById(int id) {
        topicMapper.deleteById(id);
    }

    @Override
    public List<Topic> findByTopic_title(String topic_title) {
        return topicMapper.findByTopic_title(topic_title);
    }

    @Override
    public List<Topic> findByTopic_uid(String topic_uid) {
        return topicMapper.findByTopic_uid(topic_uid);
    }

    @Override
    public String create(Topic topic) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        topic.setTopic_timestamp(simpleDateFormat.format(date));
        topicMapper.create(topic);
        return "ok";
    }

    @Override
    public Topic findById(int id) {
        return topicMapper.findById(id);
    }

    @Override
    public String edit(Topic topic) {
        try {
            topicMapper.edit(topic);
            return "ok";
        } catch (Exception e) {
            System.out.println("CourseServiceImpl.doEdit() ERROR:e=" + e);
        }
        return "error";

    }

    @Override
    public MyPageInfo findBySearch(HttpServletRequest request) {
        int currentPageNum = 1;//起始页1
        int pageSize = 10;//每页几条数据
        String currentPageNumStr = request.getParameter("pageNum");//从requst中获得pageNum参数
        if (currentPageNumStr != null) {
            currentPageNum = Integer.parseInt(currentPageNumStr);//将字符串转换成Integer类型
        }
        String topic_title = request.getParameter("topic_title");
        String topic_uid = request.getParameter("topic_uid");


        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在SQL之前执行
        topicMapper.findBySearch(topic_title, topic_uid);

        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getNavigateLastPage(), pageInfo.getTotal(), pageInfo.getList());


        myPageInfo.setSearch1(topic_title);
        myPageInfo.setSearch2(topic_uid);

        return myPageInfo;
    }

    @Override
    public MyPageInfo findBySearchUser(HttpServletRequest request) {
        int currentPageNum = 1;//起始页1
        int pageSize = 10;//每页几条数据
        String currentPageNumStr = request.getParameter("pageNum");//从requst中获得pageNum参数
        if (currentPageNumStr != null) {
            currentPageNum = Integer.parseInt(currentPageNumStr);//将字符串转换成Integer类型
        }
        String topic_title = request.getParameter("topic_title");
        String topic_uid =  String.valueOf(((User) request.getSession().getAttribute("user_info")).getUser_uid());


        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在SQL之前执行
        topicMapper.findBySearch(topic_title, topic_uid);

        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getNavigateLastPage(), pageInfo.getTotal(), pageInfo.getList());




        return myPageInfo;
    }
}
